package xgb









/*
.RqstCreatWnd:
*/
func(pip *PipT)RqstCreatWnd(width, height uint16, attrMsk WndAttrMskT, attrLst []uint32)(uint32, *ErrCookT){
	wid:=pip.GenerateId()
	screen:=pip.Screen()
	attrLen:=CountMaskBits(uint32(attrMsk))//返回value-mask中设为1的bit个数，也就是对应的value-list的成员个数
	data:=append([]byte{1,//opcode
											screen.Depth},//depth
											Uint16ToBytes(8+attrLen)...)//request length

	data=append(data, Uint32ToBytes(wid)...)//wid
	data=append(data, Uint32ToBytes(screen.Root)...)//parent

	data=append(data, 0, 0, 0, 0)//x, y uint16**************************************
	data=append(data, Uint16ToBytes(width)...)//width
	data=append(data, Uint16ToBytes(height)...)//height
	data=append(data, 0, 0)//border-width uint16

	data=append(data, Uint16ToBytes(uint16(WND_CLS_IN_OUT))...)//class
	data=append(data, 0, 0, 0, 0)//visual uint32，设为0表示继承自父窗口
	data=append(data, Uint32ToBytes(uint32(attrMsk))...)//value-mask

	if attrLen!=0{//如果value-mask中为1的bit数不为0
		for _,v:=range attrLst{//遍历value-list中所有成员
			data=append(data, Uint32ToBytes(v)...)//将每个成员都转换为bytes并追加到data结尾
		}//for
	}//if

	if pip.rqstSqc==0xffff{pip.rqstSqc=0}//如果请求序列号达到了uint16最大值0xffff则重置为0
	pip.rqstSqc++//请求序列号加1；请求序列号是从1开始的，不是从0开始的，所以重置后不用跳过加1

	cookie:=new(ErrCookT)//创建错误Cookie，所有错误格式都一样，所以错误Cookie也只有一种类型
	cookie.sqc=pip.rqstSqc//将当前请求的序列号保存进错误Cookie
	cookie.pip=pip//将当前X链接的指针保存进错误Cookie，以便Wait时可以获得X服务器处理完的最新请求的序列号

	pip.errQue[pip.rqstSqc]=&cookie.Err//错误Cookie中的Err成员是指向ErrT类型结构的指针，将指向Err成员的指针保存进错误队列中index为当前请求序列号处，则dispatch时就可以将返回的错误根据对应的请求序列号对号入座

	_,err:=pip.con.Write(data); if err!=nil{panic(err)}//将请求发送给X服务器并判断处理错误

	return wid, cookie//返回新创建窗口的ID和错误Cookie
}//RqstCreatWnd
//以下同类方法参考本方法注释



/*
.RqstCreatSubWnd:
*/
func(pip *PipT)RqstCreatSubWnd(parent uint32, x, y int16, width, height uint16, attrMsk WndAttrMskT, attrLst []uint32)(uint32, *ErrCookT){
	wid:=pip.GenerateId()
	screen:=pip.Screen()
	attrLen:=CountMaskBits(uint32(attrMsk))
	data:=append([]byte{1, screen.Depth}, Uint16ToBytes(8+attrLen)...)
	data=append(data, Uint32ToBytes(wid)...)
	data=append(data, Uint32ToBytes(parent)...)

	data=append(data, Int16ToBytes(x)...)
	//子窗口的x,y为负数或大于父窗口width,height时，子窗口会定位到父窗口范围外而不可见
	data=append(data, Int16ToBytes(y)...)

	data=append(data, Uint16ToBytes(width)...)
	data=append(data, Uint16ToBytes(height)...)
	data=append(data, 0, 0)//border-width uint16
	data=append(data, Uint16ToBytes(uint16(WND_CLS_IN_OUT))...)
	data=append(data, 0, 0, 0, 0)//visual uint32
	data=append(data, Uint32ToBytes(uint32(attrMsk))...)

	if attrLen!=0{
		for _,v:=range attrLst{
			data=append(data, Uint32ToBytes(uint32(v))...)
		}//for
	}//if

	if pip.rqstSqc==0xffff{pip.rqstSqc=0}
	pip.rqstSqc++

	cookie:=new(ErrCookT)
	cookie.sqc=pip.rqstSqc
	cookie.pip=pip
	pip.errQue[pip.rqstSqc]=&cookie.Err

	_,err:=pip.con.Write(data); if err!=nil{panic(err)}
	return wid, cookie
}//RqstCreatSubWnd




/*
.RqstCreatWindow:
*/
func(pip *PipT)RqstCreatWindow(x, y int16, width, height, borderWidth uint16, class WndClsT, parent, visual uint32, depth byte, attrMsk WndAttrMskT, attrLst []uint32)(uint32, *ErrCookT){
	wid:=pip.GenerateId()
	attrLen:=CountMaskBits(uint32(attrMsk))
	data:=append([]byte{1, depth}, Uint16ToBytes(8+attrLen)...)
	data=append(data, Uint32ToBytes(wid)...)
	data=append(data, Uint32ToBytes(parent)...)
	data=append(data, Int16ToBytes(x)...)
	data=append(data, Int16ToBytes(y)...)
	data=append(data, Uint16ToBytes(width)...)
	data=append(data, Uint16ToBytes(height)...)
	data=append(data, Uint16ToBytes(borderWidth)...)
	data=append(data, Uint16ToBytes(uint16(class))...)
	data=append(data, Uint32ToBytes(visual)...)
	data=append(data, Uint32ToBytes(uint32(attrMsk))...)

	if attrLen!=0{
		for _,v:=range attrLst{
			data=append(data, Uint32ToBytes(uint32(v))...)
		}//for
	}//if

	if pip.rqstSqc==0xffff{pip.rqstSqc=0}
	pip.rqstSqc++

	cookie:=new(ErrCookT)
  cookie.sqc=pip.rqstSqc
  cookie.pip=pip
  pip.errQue[pip.rqstSqc]=&cookie.Err

  _,err:=pip.con.Write(data); if err!=nil{panic(err)}
	return wid, cookie
}//RqstCreatWindow





/*
.RqstMapWnd:
*/
func(pip *PipT)RqstMapWnd(wid uint32)*ErrCookT{
	data:=append([]byte{8,//opcode
		0,//unused
		0, 2},//request length uint16
		Uint32ToBytes(uint32(wid))...)

	if pip.rqstSqc==0xffff{pip.rqstSqc=0}
	pip.rqstSqc++

	cookie:=new(ErrCookT)
  cookie.sqc=pip.rqstSqc
  cookie.pip=pip
  pip.errQue[pip.rqstSqc]=&cookie.Err

  _,err:=pip.con.Write(data); if err!=nil{panic(err)}
	return cookie
}//RqstMapWnd




/*
.RqstGetWndAttr:
*/
func(pip *PipT)RqstGetWndAttr(wid uint32)*GetWndAttrCookT{
	data:=append([]byte{3,//opcode
		0,//unused
		0, 2},//request length uint16
		Uint32ToBytes(wid)...)

	if pip.rqstSqc==0xffff{pip.rqstSqc=0}
	pip.rqstSqc++

//每个响应的格式都不同，所以每个有响应的Cookie都有自己的类型
	cookie:=new(GetWndAttrCookT)
	pip.errQue[pip.rqstSqc]=&cookie.Err

//每个有响应Cookie的Rpl成员都是指向对应响应类型的指针，将指向Rpl成员的指针保存进RplQueT类型结构中，再将该结构保存进响应队列中index为当前请求序列号处，dispatch时就可以将响应对号入座了
	pip.rplQue[pip.rqstSqc]=&RplQueT{
		req: 3,
		rplP: &cookie.Rpl,
	}

	_,err:=pip.con.Write(data); if err!=nil{panic(err)}
	return cookie
}//RqstGetWndAttr
//后续有响应Cookie注释请参考本方法









/*
.RqstChgWndAttr:
*/
// func(pip *PipT)RqstChgWndAttr(wid uint32, attrMsk WndAttrMskT, attrLst []uint32)